package com.lili.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.model.setting.AdminUser;
import com.lili.model.setting.vo.AdminUserSearchParam;
import com.lili.setting.mapper.UserMapper;
import com.lili.setting.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<AdminUser> getAllUser() {
        return userMapper.selectList(null);
    }

    /**
     * 添加用户
     *
     * @param adminUser
     */
    @Override
    public void saveUser(AdminUser adminUser) {
        userMapper.insert(adminUser);
    }

    /**
     * 修改用户状态
     *
     * @param userId
     * @param status
     */
    @Override
    public void changeStatus(String userId, boolean status) {
        UpdateWrapper<AdminUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", userId);
        wrapper.set("status", status);
        userMapper.update(null, wrapper);
    }

    /**
     * 删除用户信息
     *
     * @param userId
     */
    @Override
    public void deleteUser(String userId) {
        userMapper.deleteById(userId);
    }

    /**
     * 更改用户信息
     *
     * @param adminUser
     */
    @Override
    public void editUser(AdminUser adminUser) {
        userMapper.updateById(adminUser);
    }

    /**
     * 分页查询用户角色
     * @param adminUserSearchParam
     * @return
     */
    @Override
    public Map getUserPageInfo(AdminUserSearchParam adminUserSearchParam) {
        // 获取分页对象
        IPage<AdminUser> page = new Page<>(adminUserSearchParam.getPageNumber(), adminUserSearchParam.getPageSize());
        // 封装条件
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        // 获取表达式查询条件
        LambdaQueryWrapper<AdminUser> lambda = wrapper.lambda();
        // 封装条件 主打一个抽象
        lambda
                .eq(
                        adminUserSearchParam.getUsername() != null && adminUserSearchParam.getUsername().length() > 0,
                        AdminUser::getUsername,
                        adminUserSearchParam.getUsername())
                .eq(
                        adminUserSearchParam.getDepartmentId() != null && adminUserSearchParam.getDepartmentId().length() > 0,
                        AdminUser::getDepartmentId,
                        adminUserSearchParam.getDepartmentId()
                )
                .eq(
                        adminUserSearchParam.getMobile() != null && adminUserSearchParam.getMobile().length() > 0,
                        AdminUser::getMobile,
                        adminUserSearchParam.getMobile()
                )
                .orderBy(
                        adminUserSearchParam.getSort() != null && adminUserSearchParam.getOrder().length() > 0,
                        adminUserSearchParam.getSort().equals("asc"),
                        AdminUser::getCreateTime
                );


        // 进行分页查询
        userMapper.selectPage(page, lambda);
        // 封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        map.put("pageNo", page.getCurrent());
        map.put("pageSize", page.getSize());

        return map;
    }
}
